/**
 * @author Zou Chris
 */
//
// ---------------------------------------- 全局变量 ------------------------------------------------
//

var selectArticleJqueryMethod_getSearchPattern = function() { alert("selectArticleJqueryMethod_getSearchPattern is not registered"); };
var selectArticleJqueryMethod_getArticleSearchScope = function() { alert("selectArticleJqueryMethod_getArticleSearchScope is not registered"); };
var selectArticleJqueryMethod_getArticleCatagory = function() { alert("selectArticleJqueryMethod_getArticleCatagory is not registered"); };

var selectArticleJqueryMethods_callbackWhenArticleSelected = new Array();

//
//---------------------------------------- 自动执行代码 ------------------------------------------------
//

// 暂无

//
//---------------------------------------- 对外公开的函数 ------------------------------------------------
//

function selectArticleJqueryMethod_registerMethodGetSearchPattern(callback) {
	selectArticleJqueryMethod_getSearchPattern = callback;
}

function selectArticleJqueryMethod_registerMethodGetArticleSearchScope(callback) {
	selectArticleJqueryMethod_getArticleSearchScope = callback;
}

function selectArticleJqueryMethod_registerMethodGetArticleCatagory(callback) {
	selectArticleJqueryMethod_getArticleCatagory = callback;
}


function selectArticleJqueryMethod_registerMethodCallbackWhenArticleSelected(callback) {
	selectArticleJqueryMethods_callbackWhenArticleSelected.push(callback);
}

function selectArticleJqueryMethod_selectArticleImplementaion(pageNum, pgViewEncode) {
	var searchPattern = selectArticleJqueryMethod_getSearchPattern();
	var positionScope = selectArticleJqueryMethod_getArticleSearchScope(); 	// 返回四个值 minX, maxX, minY, maxY
	var articleCatagory = selectArticleJqueryMethod_getArticleCatagory();
	
	//下面是PageNum,需要注意在点击只看标签或者查看全部等涉及到三联动的时候需要对pageNum进行处理，如设置为1等
	var pageNumber = 1;
	if (pageNum){
		pageNumber = pageNum;
	}
	
	var pgViewEncodeStr = "";
	if (pgViewEncode){
		pgViewEncodeStr = pgViewEncode;
	}
	
	 //alert("Select_article_by_jquery start to fetch data, articleCatagory=" + articleCatagory + ", searchPattern=" + searchPattern + ", minX=" + minX + ", minY=" + minY + ", maxX=" + maxX + ", maxY=" + maxY + ", pageNumber=" + pageNumber + ", pgViewEncodeStr=" + pgViewEncodeStr);
	$.ajax({
		type: "post",
		url:"../article/ajaxSelectArticle",
		data: {
			   "articleCatagory": articleCatagory,
			   "searchPattern": searchPattern,
		       "minX": positionScope[0],
		       "maxX": positionScope[1],
		       "minY": positionScope[2],
		       "maxY": positionScope[3],
		       "pageNumber": pageNumber,
		       "pgViewEncodeStr" : pgViewEncodeStr
		}, 
		dateType: "json",
		success: function(res) {
			if(res.result == 0) {
				//alert("Select_article_by_jquery got data successfully, size=" + res.list.length);
				callbackMethodsWhenArticleSelected(res);
			}
			else {
				alert("系统获取信息错误！" + res.message);			
			}
		}
	});
}


//
//---------------------------------------- 私有函数 ------------------------------------------------
//

//回调所有需要更新文章的方法
function callbackMethodsWhenArticleSelected(dataSet) {
	if (selectArticleJqueryMethods_callbackWhenArticleSelected.length == 0) {
		alert("selectArticleJqueryMethods_callbackWhenArticleSelected have no method registered");
	}
	else {
		for(var i=0; i<selectArticleJqueryMethods_callbackWhenArticleSelected.length; i++) {
			selectArticleJqueryMethods_callbackWhenArticleSelected[i](dataSet);
		}
		
	}
}